﻿//*********************************************************************
//
//  文件名：BranchController
//
//  作者： Pwp
//
//  作用：机构管理模块
//
//  日期：2015-09-15
//
//*********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Application.WebAPI.Filter;
using YouTravel.DomainService;
using Framework.IOC;
using Framework.Common;
using Framework.Common.Paged;
using YouTravel.QueryModels;
using YouTravel.ViewModels;
using YouTravel.Framework.ResourceMessage;
using YouTravel.DomainModels;
using YouTravel.Framework.Enums;
using Framework.Common.Exceptions;

namespace Application.WebAPI.Controllers
{
    [RoutePrefix("api/branch")]
    public class BranchController : BaseController
    {
        IBranchService branchService;
        DataResult dataResult = new DataResult();
        public BranchController(IBranchService branchService)
            : base()
        {
            this.branchService = branchService;
        }
        /// <summary>
        /// 机构列表
        /// </summary>
        /// <param name="inputBranch"></param>
        /// <returns></returns>
        [Route("branchlist")]
        [HttpPost]
		public DataResult GetBranchList(PagedParam<BranchQuery> inputBranch)
        {
			#region 角色验证
			//总公司访问
			if(LoginUser.Type != 0&& LoginUser.Type != UserType.Company ){
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			if (string.IsNullOrEmpty(inputBranch.QueryDto.Type))
				throw new DataValidationException(string.Format(BusinessResourceMessage.ItemCanNotNull, "机构类型"));
			#endregion

			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			dataResult.Data = branchService.GetList(inputBranch);
			return dataResult;
        }
        /// <summary>
        /// 添加机构
        /// </summary>
        /// <param name="inputBranch"></param>
        /// <returns></returns>
        [Route("addbranch")]
		[HttpPost]
		public DataResult AddBranch(BranchViewModel inputBranch)
		{
			#region 角色验证
			//总公司访问
			if (LoginUser.Type != 0&&LoginUser.Type != UserType.Company)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			if (string.IsNullOrEmpty(inputBranch.Name))
				throw new DataValidationException(string.Format(BusinessResourceMessage.ItemCanNotNull, "机构名称"));
			if (inputBranch.Name.Length>50)
				throw new DataValidationException(string.Format(BusinessResourceMessage.ItemLenError, "机构名称", "50"));
			if (!CommonValidator.isMoney(inputBranch.GrossMargin.ToString()))
				throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "专线毛利率"));
			if (!CommonValidator.isMoney(inputBranch.CounterFeeRate.ToString()))
				throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "付款手续费率"));
			if (!CommonValidator.isMoney(inputBranch.ArrearsTotal.ToString()))
				throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "允许欠款总额"));
			if (!CommonValidator.isInt(inputBranch.OweTicketsDay.ToString()))
				throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "允许欠票周期"));
			#endregion

			inputBranch.OperatorID = LoginUser.ID;
			inputBranch.OperatorName = LoginUser.Name;
			inputBranch.OptionTime = DateTime.Now;
			branchService.AddBranch(inputBranch);
			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			return dataResult;
        }

		/// <summary>
		/// 查看机构详细
		/// </summary>
		/// <param name="inputBranch"></param>
		/// <returns></returns>
		[Route("detailbranch")]
		[HttpPost]
		public DataResult DetailBranch(Branch inputBranch)
		{
			#region 角色验证
			//总公司/分社访问
			if (LoginUser.Type != 0 && LoginUser.Type == UserType.SpecialLine)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			//判断菜单进入、选择分社配置进入
			if (inputBranch.ID == 0)
			{
				//验证菜单进入用户是否有打开分社配置权限
				if (LoginUser.BranchId != null && LoginUser.Type == UserType.Branch)
					inputBranch.ID = (int)LoginUser.BranchId;
				else
					throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			dataResult.Data = branchService.DetailBranch(inputBranch.ID);
			return dataResult;
		}

		/// <summary>
		/// 编辑机构基础信息
		/// </summary>
		/// <param name="inputBranch"></param>
		/// <returns></returns>
		[Route("editbranch")]
		[HttpPost]
		public DataResult EditBranch(BranchViewModel inputBranch)
		{
			#region 角色验证
			//总公司/分社访问
			if (LoginUser.Type != 0&& LoginUser.Type == UserType.SpecialLine)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			//判断菜单进入、选择分社配置进入
			if (inputBranch.ID == 0)
			{
				//验证菜单进入用户是否有打开分社配置权限
				if (LoginUser.BranchId != null && LoginUser.Type == UserType.Branch)
					inputBranch.ID = (int)LoginUser.BranchId;
				else
					throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			//单独变更、全部变更判断
			switch (inputBranch.Type)
			{
				case 1:
					if (!CommonValidator.isMoney(inputBranch.GrossMargin.ToString()))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "专线毛利率"));
					break;
				case 2:
					if (!CommonValidator.isMoney(inputBranch.CounterFeeRate.ToString()))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "付款手续费率"));
					break;
				case 3:
					if (!CommonValidator.isMoney(inputBranch.ArrearsTotal.ToString()))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "允许欠款总额"));
					break;
				case 4:
					if (!CommonValidator.isInt(inputBranch.OweTicketsDay.ToString()))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "允许欠票周期"));
					break;
				case 0:
				default:
					if (!CommonValidator.isMoney(inputBranch.GrossMargin.ToString()))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "专线毛利率"));
					if (!CommonValidator.isMoney(inputBranch.CounterFeeRate.ToString()))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "付款手续费率"));
					if (!CommonValidator.isMoney(inputBranch.ArrearsTotal.ToString()))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "允许欠款总额"));
					if (!CommonValidator.isInt(inputBranch.OweTicketsDay.ToString()))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "允许欠票周期"));
					break;
			}
			#endregion

			inputBranch.OperatorID = LoginUser.ID;
			inputBranch.OperatorName = LoginUser.Name;
			inputBranch.OptionTime = DateTime.Now;
			branchService.EditBranch(inputBranch);
			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			return dataResult;
		}

		/// <summary>
		/// 变更机构状态（冻结，解冻）
		/// </summary>
		/// <param name="inputBranch"></param>
		/// <returns></returns>
		[Route("frozenstatus")]
		[HttpPost]
		public DataResult FrozenStatus(Branch inputBranch)
		{
			#region 角色验证
			//总公司访问
			if (LoginUser.Type != 0 && LoginUser.Type != UserType.Company)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			if (!CommonValidator.isInt(inputBranch.ID.ToString()))
				throw new DataValidationException(string.Format(BusinessResourceMessage.Inexistent, "分社"));
			#endregion

			inputBranch.UpdaterId = LoginUser.ID;
			inputBranch.UpdaterName = LoginUser.Name;
			inputBranch.UpdateTime = DateTime.Now;
			branchService.FrozenBranch(inputBranch);
			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			return dataResult;
		}

		/// <summary>
		/// 添加扣款类型
		/// </summary>
		/// <param name="inputDeduction"></param>
		/// <returns></returns>
		[Route("adddeduction")]
		[HttpPost]
		public DataResult AddDeduction(Deduction inputDeduction)
		{
			#region 角色验证
			//分社访问
			if (LoginUser.Type != 0&& LoginUser.Type != UserType.Branch)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			//判断菜单进入、选择分社配置进入
			if (inputDeduction.BranchId == 0)
			{
				//验证菜单进入用户是否有打开分社配置权限
				if (LoginUser.BranchId != null && LoginUser.Type == UserType.Branch)
					inputDeduction.BranchId = (int)LoginUser.BranchId;
				else
					throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			if (string.IsNullOrEmpty(inputDeduction.Name))
				throw new DataValidationException(string.Format(BusinessResourceMessage.ItemCanNotNull, "扣款类型名称"));
			#endregion

			DateTime dtNow = DateTime.Now;
			inputDeduction.CreaterId = LoginUser.ID;
			inputDeduction.CreateTime = dtNow;
			inputDeduction.UpdaterId = LoginUser.ID;
			inputDeduction.UpdateTime = dtNow;
			branchService.AddDeduction(inputDeduction);
			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			dataResult.Data = inputDeduction.ID;
			return dataResult;
		}

		/// <summary>
		/// 删除扣款类型
		/// </summary>
		/// <param name="inputDeduction"></param>
		/// <returns></returns>
		[Route("editdeduction")]
		[HttpPost]
		public DataResult DelDeduction(Deduction inputDeduction)
		{
			#region 角色验证
			//分社访问
			if (LoginUser.Type != 0&& LoginUser.Type != UserType.Branch)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			if (!CommonValidator.isInt(inputDeduction.ID.ToString()))
				throw new DataValidationException(string.Format(BusinessResourceMessage.Inexistent, "扣款类型"));
			#endregion

			inputDeduction.UpdaterId = LoginUser.ID;
			inputDeduction.UpdateTime = DateTime.Now;
			branchService.DelDeduction(inputDeduction);
			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			return dataResult;
		}

		/// <summary>
		/// 添加发票科目
		/// </summary>
		/// <param name="inputInvoiceSubject"></param>
		/// <returns></returns>
		[Route("addinvoicesubject")]
		[HttpPost]
		public DataResult AddInvoiceSubject(InvoiceSubject inputInvoiceSubject)
		{
			#region 角色验证
			//分社访问
			if (LoginUser.Type != 0&& LoginUser.Type != UserType.Branch)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			//判断菜单进入、选择分社配置进入
			if (inputInvoiceSubject.BranchId == 0)
			{
				//验证菜单进入用户是否有打开分社配置权限
				if (LoginUser.BranchId != null && LoginUser.Type == UserType.Branch)
					inputInvoiceSubject.BranchId = (int)LoginUser.BranchId;
				else
					throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			if (string.IsNullOrEmpty(inputInvoiceSubject.InvoiceSubjectName))
				throw new DataValidationException(string.Format(BusinessResourceMessage.ItemCanNotNull, "发票科目名称"));
			#endregion

			DateTime dtNow = DateTime.Now;
			inputInvoiceSubject.CreaterId = LoginUser.ID;
			inputInvoiceSubject.CreateTime = dtNow;
			inputInvoiceSubject.UpdaterId = LoginUser.ID;
			inputInvoiceSubject.UpdateTime = dtNow;
			branchService.AddInvoiceSubject(inputInvoiceSubject);
			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			dataResult.Data = inputInvoiceSubject.ID;
			return dataResult;
		}

		/// <summary>
		/// 删除发票科目
		/// </summary>
		/// <param name="inputInvoiceSubject"></param>
		/// <returns></returns>
		[Route("editinvoicesubject")]
		[HttpPost]
		public DataResult DelInvoiceSubject(InvoiceSubject inputInvoiceSubject)
		{
			#region 角色验证
			//分社访问
			if (LoginUser.Type != 0&& LoginUser.Type != UserType.Branch)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			if (!CommonValidator.isInt(inputInvoiceSubject.ID.ToString()))
				throw new DataValidationException(string.Format(BusinessResourceMessage.Inexistent, "发票科目"));
			#endregion

			inputInvoiceSubject.UpdaterId = LoginUser.ID;
			inputInvoiceSubject.UpdateTime = DateTime.Now;
			branchService.DelInvoiceSubject(inputInvoiceSubject);
			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			return dataResult;
		}
    }
}